import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../components/login.vue')
const Home = () => import('../components/home.vue')
const Welcome = () => import('../components/Welcome.vue')
const Users = () => import('../components/users/users.vue')
const Rights = () => import('../components/power/rights.vue')
const Roles = () => import('../components/power/roles.vue')
const Cate = () => import('../components/goods/cate.vue')
const Params = () => import('../components/goods/params.vue')
const List = () => import('../components/goods/list.vue')
const Add = ()=> import('../components/goods/add.vue')
const Order = ()=> import('../components/order/order.vue')
const Report = ()=> import('../components/report/report.vue')
Vue.use(VueRouter)


const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  },
]


const router = new VueRouter({
  routes
})

// 挂载路由导航

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
  }
})

export default router
